import { Directive, Input } from "@angular/core";
import { NG_VALIDATORS, Validator, AbstractControl } from "@angular/forms";

@Directive({
  selector: "[appEqualValidator]",
  providers: [
    {
      provide: NG_VALIDATORS,
      useExisting: EqualValidatorDirective,
      multi: true
    }
  ]
})
export class EqualValidatorDirective implements Validator {
  @Input() validateEqual: string;
  @Input() reverse: boolean;

  constructor() {}

  validate(control: AbstractControl): { [key: string]: any } {
    let selfValue = control.value;
    let targetControl = control.root.get(this.validateEqual);
    if (targetControl && selfValue !== targetControl.value) {
      if (!this.reverse) {
        return { validateEqual: false };
      } else {
        targetControl.setErrors({
          validateEqual: false
        });
      }
    } else {
      targetControl.setErrors(null);
    }
    return null;
  }
}
